import 'package:codemode/utils/colors.dart';
import 'package:codemode/views/TextFactory.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../AppModel.dart';

class home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _home();
  }
}

// FractionallySizedBox 1/10
class _home extends State<home> {
  String appName = "";

  var currentPanelIndex = -1; // 当前panel的index
  List<int> mList; // 组成一个int类型数组，用来控制索引
  List<bool> expandStateList;

  //tab list data
    List<>

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    mList = [];
    expandStateList = [];
    // 遍历为两个List进行赋值
    for (int i = 0; i < 10; i++) {
      mList.add(i);
      expandStateList.add(false);
    }

    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      setState(() {
        appName = packageInfo.appName;
      });
      //     packageInfo.appName;
      // String packageName = packageInfo.packageName;
      // String version = packageInfo.version;
      // String buildNumber = packageInfo.buildNumber;
    });
  }

  @override
  Widget build(BuildContext context) {
    AppModel.initScreen(context);
    return Material(
        child: Row(
      children: [_left(), _rightView],
    ));
  }

  _left() {
    return Container(
      color: MyColors.mainbgcolor,
      width: 100.w,
      child: Column(
        children: [
          Container(
            color: Colors.blueAccent,
            width: double.infinity,
            alignment: Alignment.center,
            height: 80.h,
            child: Text(
              appName,
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 30.sp, color: Colors.white),
            ),
          ),
          Container(
            width: double.infinity,
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.fromLTRB(2.w, 8.w, 2.w, 0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: 20.w,
                        height: 20.w,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          image: DecorationImage(
                            fit: BoxFit.fill,
                            image: NetworkImage(
                                AppModel.instance.getUserModel.avatar),
                          ),
                        ),
                      ),
                      Expanded(
                          child: Container(
                        height: 70.h,
                        margin: EdgeInsets.fromLTRB(2.w, 0, 0, 0),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            TextFactory.getAppText(
                                "" + AppModel.instance.getUserModel.userName),
                            TextFactory.getAppText(
                                "" + AppModel.instance.getUserModel.deptName),
                          ],
                        ),
                      ))
                    ],
                  ),
                ),
              ],
            ),
          ),
          Expanded(child: _leftList),
        ],
      ),
    );
  }

  get _rightView => Expanded(
          child: Column(
        children: [_rightviewbat],
      ));

  get _rightviewbat => TabBar(tabs: tabs);

  // this.children = const <ExpansionPanel>[], //子集
  // this.expansionCallback, //展开闭合的回调
  // this.animationDuration = kThemeAnimationDuration, //动画时长
  get _leftList => SingleChildScrollView(
      padding: EdgeInsets.fromLTRB(0, 8.w, 0, 0),
      child: ExpansionPanelList(
        elevation: 0,
        expansionCallback: (int panelIndex, bool isExpanded) {
          print("expansionCallback $panelIndex  $isExpanded");
          setState(() {
            for (int i = 0; i < expandStateList.length; i++) {
              expandStateList[i] = false;
            }
            expandStateList[panelIndex] = !isExpanded;
          });
        },
        children: mList.map((index) {
          return ExpansionPanel(
              backgroundColor: expandStateList[index]
                  ? MyColors.mainbgselect
                  : MyColors.mainbgcolor,
              canTapOnHeader: true,
              headerBuilder: (context, isExpanded) {
                return Container(
                  height: 40.h,
                  child: ListTile(
                      title: Text(
                    '优帆远扬$index',
                    style: TextStyle(color: MyColors.maintext),
                  )),
                );
              },
              body: Container(
                height: 200,
                padding: EdgeInsets.symmetric(horizontal: 5.0),
                child: new ListView.builder(
                    itemCount: 5,
                    itemBuilder: (BuildContext context, int position) {
                      return ListTile(
                          onTap: () => _leftitemclick(position),
                          title: Text('优帆远扬$index',
                              style: TextStyle(color: MyColors.maintext)));
                    }),
              ),
              isExpanded: expandStateList[index]);
        }).toList(),
      ));

  _leftitemclick(int clickindex) {
    Fluttertoast.showToast(msg: "点击$clickindex");
  }
}
